﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;

namespace EIA.SLClient.Utility.Behaviors
{
    public class Background4StateBehavior : Behavior<Control>
    {
        private Brush brush = null;
        protected override void OnAttached()
        {
            base.OnAttached();
            if (AssociatedObject != null)
            {
                brush = this.AssociatedObject.Background;
                RepresendBackgroundAsync();
                this.AssociatedObject.MouseEnter += OnMouseEnter;
                this.AssociatedObject.MouseLeave += OnMouseLeave;
                this.AssociatedObject.MouseLeftButtonDown += OnMouseLeftButtonDown;
                this.AssociatedObject.MouseLeftButtonUp += OnMouseLeftButtonUp;
                this.AssociatedObject.IsEnabledChanged += OnIsEnabledChanged;
                this.AssociatedObject.LostMouseCapture += OnLostMouseCapture;
            }
        }
        protected override void OnDetaching()
        {
            base.OnDetaching();
            if (AssociatedObject != null)
            {
                this.AssociatedObject.MouseEnter -= OnMouseEnter;
                this.AssociatedObject.MouseLeave -= OnMouseLeave;
                this.AssociatedObject.MouseLeftButtonDown -= OnMouseLeftButtonDown;
                this.AssociatedObject.MouseLeftButtonUp -= OnMouseLeftButtonUp;
                this.AssociatedObject.IsEnabledChanged -= OnIsEnabledChanged;
                this.AssociatedObject.Background = brush;
                this.AssociatedObject.LostMouseCapture -= OnLostMouseCapture;
            }
        }

        protected void RepresentBackground()
        {
            if (this.AssociatedObject.Visibility != Visibility.Visible)
            {
                this.AssociatedObject.Background = BackgroundNormal;
                this._is_mouse_enter = false;
                this._is_left_mouse_down = false;
                return;
            }
            if (!this.AssociatedObject.IsEnabled) this.AssociatedObject.Background = BackgroundDisabled;
            else
            {
                if (_is_left_mouse_down)
                {
                    this.AssociatedObject.Background = BackgroundPressed;
                }
                else
                {
                    if (_is_mouse_enter)
                    {
                        this.AssociatedObject.Background = BackgroundMouseOver;
                    }
                    else
                    {
                        this.AssociatedObject.Background = BackgroundNormal;
                    }
                }
            }
        }

        protected void RepresendBackgroundAsync()
        {
            this.AssociatedObject.Dispatcher.BeginInvoke(RepresentBackground);
        }

        protected void OnLostMouseCapture(object sender, EventArgs e)
        {
            RepresendBackgroundAsync();
        }

        protected void OnIsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            RepresendBackgroundAsync();
        }

        protected void OnMouseEnter(object sender, MouseEventArgs e)
        {
            _is_mouse_enter = true;
            RepresendBackgroundAsync();
        }
        protected void OnMouseLeave(object sender, MouseEventArgs e)
        {
            _is_mouse_enter = false;
            RepresendBackgroundAsync();
        }
        protected void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _is_left_mouse_down = true;
            RepresendBackgroundAsync();
        }
        protected void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _is_left_mouse_down = false;
            RepresendBackgroundAsync();
        }
        bool _is_left_mouse_down = false;
        bool _is_mouse_enter = false;

        #region BackgroundNormal (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public Brush BackgroundNormal
        {
            get { return (Brush)GetValue(BackgroundNormalProperty); }
            set { SetValue(BackgroundNormalProperty, value); }
        }
        public static readonly DependencyProperty BackgroundNormalProperty =
            DependencyProperty.Register("BackgroundNormal", typeof(Brush), typeof(Background4StateBehavior),
            new PropertyMetadata(null, new PropertyChangedCallback(OnBackgroundNormalChanged)));

        private static void OnBackgroundNormalChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            Background4StateBehavior source = (Background4StateBehavior)sender;

            // 处理逻辑
            Brush newValue = (Brush)args.NewValue;
        }

        #endregion

        #region BackgroundMouseOver (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public Brush BackgroundMouseOver
        {
            get { return (Brush)GetValue(BackgroundMouseOverProperty); }
            set { SetValue(BackgroundMouseOverProperty, value); }
        }
        public static readonly DependencyProperty BackgroundMouseOverProperty =
            DependencyProperty.Register("BackgroundMouseOver", typeof(Brush), typeof(Background4StateBehavior),
            new PropertyMetadata(null, new PropertyChangedCallback(OnBackgroundMouseOverChanged)));

        private static void OnBackgroundMouseOverChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            Background4StateBehavior source = (Background4StateBehavior)sender;

            // 处理逻辑
            Brush newValue = (Brush)args.NewValue;
        }

        #endregion

        #region BackgroundPressed (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public Brush BackgroundPressed
        {
            get { return (Brush)GetValue(BackgroundPressedProperty); }
            set { SetValue(BackgroundPressedProperty, value); }
        }
        public static readonly DependencyProperty BackgroundPressedProperty =
            DependencyProperty.Register("BackgroundPressed", typeof(Brush), typeof(Background4StateBehavior),
            new PropertyMetadata(null, new PropertyChangedCallback(OnBackgroundPressedChanged)));

        private static void OnBackgroundPressedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            Background4StateBehavior source = (Background4StateBehavior)sender;

            // 处理逻辑
            Brush newValue = (Brush)args.NewValue;
        }

        #endregion

        #region BackgroundDisabled (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public Brush BackgroundDisabled
        {
            get { return (Brush)GetValue(BackgroundDisabledProperty); }
            set { SetValue(BackgroundDisabledProperty, value); }
        }
        public static readonly DependencyProperty BackgroundDisabledProperty =
            DependencyProperty.Register("BackgroundDisabled", typeof(Brush), typeof(Background4StateBehavior),
            new PropertyMetadata(null, new PropertyChangedCallback(OnBackgroundDisabledChanged)));

        private static void OnBackgroundDisabledChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            Background4StateBehavior source = (Background4StateBehavior)sender;

            // 处理逻辑
            Brush newValue = (Brush)args.NewValue;
        }

        #endregion
    }
}
